﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MRS.FrontEnd.BO;

namespace Microsoft.Reputation.Services
{
    public enum RequestType
    {
        UrlDataBlock,
        UrlOverride
    }

    /// <summary>
    /// Abstract base class that it used for converting LookupRequests into DPLookupRequests
    /// Owns an ObjectType and a RequestType for operations that need to do specific work 
    /// based on the lookup type
    /// </summary>
    public abstract class DPLookupRequest
    {
        public abstract ObjectType ObjectType { get; }
        public abstract RequestType RequestType { get; }

        internal static List<DPLookupRequest> ConvertToDPLookupRequests(LookupRequest[] lookupRequests)
        {
            List<DPLookupRequest> dsRequests = new List<DPLookupRequest>(lookupRequests.Length);
            foreach (LookupRequest request in lookupRequests)
            {
                DPLookupRequest dpLookupRequest;

                byte[] datablock;
                if (request.HasDataBlock(out datablock))
                {
                    dpLookupRequest = new DPLookupUrlDataBlock
                    {
                        DataBlock = datablock
                    };
                }
                else
                {
                    // Override Request
                    List<ObjectIdentifier> objectIds = new List<ObjectIdentifier>();

                    foreach (Metadata metadata in request.SearchableFields)
                    {
                        if (metadata.Name.Equals("SHA256", StringComparison.InvariantCultureIgnoreCase))
                        {
                            objectIds.Add(new ObjectIdentifier { Type = (ObjectType)request.Type, Sha256 = metadata.Value as byte[] });
                        }
                    }
                    dpLookupRequest = new DPLookupUrlOverrides
                    {
                        Overrides = objectIds
                    };
                }
                dsRequests.Add(dpLookupRequest);
            }

            return dsRequests;
        }
    }

    /// <summary>
    /// Lookup Request for URL DataBlocks
    /// </summary>
    public class DPLookupUrlDataBlock : DPLookupRequest
    {
        public byte[] DataBlock { get; set; }
        public override ObjectType ObjectType { get { return ObjectType.URL; } }
        public override RequestType RequestType { get { return RequestType.UrlDataBlock; } }
    }

    /// <summary>
    /// Lookup Request for URL Overrides that need to be sent to external clients of the 
    /// Delivery Service.
    /// </summary>
    public class DPLookupUrlOverrides : DPLookupRequest
    {
        public List<ObjectIdentifier> Overrides { get; set; }
        public override ObjectType ObjectType { get { return ObjectType.URL; } }
        public override RequestType RequestType { get {  return RequestType.UrlOverride; } }
    }
}
